/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.admin.client.components;

import com.elf.gwt.admin.client.AdminComponentController;
import com.elf.gwt.common.client.components.CommonSimpleComponent;
import com.elf.gwt.common.client.model.GroupModel;
import com.google.gwt.core.client.GWT;

/**
 *
 */
public class CatalogComponent extends GroupMemberComponent
{	

	/**
	 * Constructor
	 */
	public CatalogComponent(AdminComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix);
		this.setGroupComponentHandle("CatalogGroupComponent");
	}

	public boolean updateFromHistoryParameters(String[] params)
	{
		boolean returnValue = super.updateFromHistoryParameters(params);
		if (params.length > 1)
		{			
			GroupModel groupModel = getSelectedGroup();
			AdminSimpleComponent component = null;
			if (groupModel.getGroup().equals("categories_products")) {
				component = getComponent("CategoriesProductsComponent");
			}
			else if (groupModel.getGroup().equals("discount_categories")) {
				component = getComponent("DiscountCategoriesComponent");
			}
			else if (groupModel.getGroup().equals("product_options")) {
				component = getComponent("ProductOptionsComponent");
			}
			else if (groupModel.getGroup().equals("product_options_values")) {
				component = getComponent("ProductOptionsValuesComponent");
			}
			else if (groupModel.getGroup().equals("product_options_to_values")) {
				component = getComponent("ProductOptionsToValuesComponent");
			}
			else if (groupModel.getGroup().equals("manufacturers")) {
				component = getComponent("ManufacturersAdministrativeComponent");
			}
			else if (groupModel.getGroup().equals("reviews")) {
				component = getComponent("ReviewsComponent");
			}
			else if (groupModel.getGroup().equals("specials")) {
				component = getComponent("SpecialsComponent");
			}
			else if (groupModel.getGroup().equals("products_expected")) {
				component = getComponent("ExpectedProductsComponent");
			}
			else if (groupModel.getGroup().equals("coupons")) {
				component = getComponent("CouponsComponent");
			}

			if (component == null) {
				GWT.log("unknown catalog group " + groupModel.getGroup(), null);
			}
			else {
				component.updateFromHistoryParameters(params);
			}
		}

		return returnValue;
	}

	public AdminSimpleComponent getComponent(String handle) {
		CommonSimpleComponent component = controller.getComponent(handle, false);
		if (component == null) {
			if (handle.equals("CategoriesProductsComponent")) {
				component = new CategoriesProductsComponent(adminController, null);
			}
			else if (handle.equals("CouponsComponent")) {
				component = new CouponsComponent(adminController, null);
			}
			else if (handle.equals("DiscountCategoriesComponent")) {
				component = new DiscountCategoriesComponent(adminController, null);
			}
			else if (handle.equals("EditProductComponent")) {
				component = new EditProductComponent(adminController, null);
			}
			else if (handle.equals("EditProductXsellComponent")) {
				component = new EditProductXsellComponent(adminController, null);
			}
			else if (handle.equals("ExpectedProductsComponent")) {
				component = new ExpectedProductsComponent(adminController, null);
			}
			else if (handle.equals("ManufacturersAdministrativeComponent")) {
				component = new ManufacturersAdministrativeComponent(adminController, null);
			}
			else if (handle.equals("ProductOptionsComponent")) {
				component = new ProductOptionsComponent(adminController, null);
			}
			else if (handle.equals("ProductOptionsValuesComponent")) {
				component = new ProductOptionsValuesComponent(adminController, null);
			}
			else if (handle.equals("ProductOptionsToValuesComponent")) {
				component = new ProductOptionsToValuesComponent(adminController, null);
			}
			else if (handle.equals("ReviewsComponent")) {
				component = new ReviewsComponent(adminController, null);
			}
			else if (handle.equals("SpecialsComponent")) {
				component = new SpecialsComponent(adminController, null);
			}
			if (component != null) {
				controller.getDisplayPanel().add(component);
				controller.setComponent(component);
			}
		}
		return (AdminSimpleComponent)component;
	}

}
